package cn.hl.system.controller;

import cn.hl.basic.util.AjaxResult;
import cn.hl.basic.util.PageList;
import cn.hl.system.domain.DictionaryDetail;
import cn.hl.system.query.DictionaryDetailQuery;
import cn.hl.system.service.IDictionaryDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dictionaryDetail")
@Api(value = "数据字典明细的API",description="数据字典明细相关的CRUD功能")
public class DictionaryDetailController {

    /** 调service层 */
    @Autowired
    private IDictionaryDetailService dictionaryDetailService;

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询")
    public List<DictionaryDetail> findById(@PathVariable("id") Long id) {
        return dictionaryDetailService.findByTypesId(id);
    }

    @GetMapping
    @ApiOperation(value = "查询所有")
    public List<DictionaryDetail> findById() {
        return dictionaryDetailService.findAll();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            dictionaryDetailService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败！");
        }
    }

    @PutMapping
    @ApiOperation(value = "添加或修改")
    public AjaxResult addAndUpdate(@RequestBody DictionaryDetail dictionaryDetail) {
        try {
            if (dictionaryDetail.getId() == null) {
                dictionaryDetailService.add(dictionaryDetail);
            } else {
                dictionaryDetailService.update(dictionaryDetail);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败！");
        }
    }

    @PostMapping
    @ApiOperation(value = "分页查询")
    public PageList<DictionaryDetail> queryPage(@RequestBody DictionaryDetailQuery dictionaryDetailQuery) {
            return dictionaryDetailService.queryPage(dictionaryDetailQuery);
    }

    @PatchMapping
    @ApiOperation(value = "批量删除")
    public AjaxResult patchDelete(@RequestBody List<Long> ids) {
        try {
            dictionaryDetailService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败！");
        }
    }
}
